/**
 * @Title: UserModal.js
 * @Description: UserModal
 * @author: Fanchao
 * @date: 2020/8/1 11:54 下午
 * @version V1.0
 */

import React, { memo, useState } from 'react';
import { connect } from 'umi';
import { Cascader, DatePicker, Form, Input, message, Modal, Select } from 'antd';
import moment from 'moment';
import { getRunTrainTaskInfoList } from 'api/index';

const layout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 20 },
};
const { Option } = Select;

const UserModal = memo(({ students: { itemData, treeData, teachClassList, trainingTypeList }, dispatch, title, visible }) => {
    const [form] = Form.useForm();
    const [runTrainTaskInfoList, setRunTrainTaskInfoList] = useState([]);
    form.setFieldsValue({
        credentialsNo: itemData.credentialsNo,
        loginId: itemData.loginId,
        realName: itemData.studentRealName,
    });
    const fetchRunTrainTaskInfoList = async (filter) => {
        const result = await getRunTrainTaskInfoList({ trainingTypeId: filter });
        if (result.status === 'success') {
            const runTrainTaskInfoList = result.result;
            setRunTrainTaskInfoList(runTrainTaskInfoList);
        } else {
            message.error(result.message);
        }
    };
    return (
        <Modal
            title={title}
            visible={visible}
            // width="900px"
            onOk={() => {
                form.validateFields().then(values => {
                    if (itemData.studentUserId) {
                        values.studentUserId = itemData.studentUserId;
                        values.studentUserName = values.realName;
                        dispatch({
                            type: 'students/updateStudentUserInfo',
                            payload: values,
                            callback: (result) => {
                                if (result) {
                                    form.resetFields();
                                    dispatch({
                                        type: 'students/setItemData',
                                        payload: {},
                                    });
                                    dispatch({
                                        type: 'students/setModalVisible',
                                        payload: false,
                                    });
                                    dispatch({
                                        type: 'students/getDataSource',
                                        payload: {
                                            keyword: '',
                                            reqPageInfo: {
                                                pageNumber: 1,
                                                pageSize: 10,
                                            },
                                        },
                                    });
                                }
                            },
                        });
                    } else {
                        values.userRoleType = 'Student';
                        values.enterSchoolDate = moment(values.enterSchoolDate).format('YYYY-MM-DD');
                        values.leastDepartmentId = values.leastDepartmentId.pop();
                        delete values.trainingTypeId;
                        dispatch({
                            type: 'students/addStudent',
                            payload: values,
                            callback: (result) => {
                                if (result) {
                                    form.resetFields();
                                    dispatch({
                                        type: 'students/setItemData',
                                        payload: {},
                                    });
                                    dispatch({
                                        type: 'students/setModalVisible',
                                        payload: false,
                                    });
                                    dispatch({
                                        type: 'students/getDataSource',
                                        payload: {
                                            keyword: '',
                                            reqPageInfo: {
                                                pageNumber: 1,
                                                pageSize: 10,
                                            },
                                        },
                                    });
                                }
                            },
                        });
                    }
                }).catch(info => {
                });
            }}
            onCancel={() => {
                form.resetFields();
                dispatch({
                    type: 'students/setItemData',
                    payload: {},
                });
                dispatch({
                    type: 'students/setModalVisible',
                    payload: false,
                });
            }}
        >
            <Form
                {...layout}
                layout="horizontal"
                form={form}
                name="basic"
                // 表单默认值，只有初始化以及重置时生效 莫用
                initialValues={{
                    // name: itemData.name
                }}
            >
                <Form.Item
                    label="培训类型"
                    name="trainingTypeId"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Select
                        placeholder="培训类型"
                        onChange={(e) => {
                            fetchRunTrainTaskInfoList(e);
                        }}
                    >
                        {
                            trainingTypeList.map(item => {
                                return <Option key={item.trainTypeId}
                                               value={item.trainTypeId}>{item.trainingTypeName}</Option>;
                            })
                        }
                    </Select>
                </Form.Item>
                <Form.Item
                    label="任务运行"
                    name="runTrainTaskId"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Select
                        placeholder="任务运行信息"
                    >
                        {
                            runTrainTaskInfoList.map(item => {
                                return <Option key={item.runTrainTaskId}
                                               value={item.runTrainTaskId}>{item.majorSysCode} {item.majorName} {item.startDate}至{item.endDate}</Option>;
                            })
                        }
                    </Select>
                </Form.Item>
                <Form.Item
                    label="学生姓名"
                    name="realName"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Input placeholder="请输入学生姓名"/>
                </Form.Item>
                <Form.Item
                    label="登录账号"
                    name="loginId"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Input placeholder="请输入登录账号"/>
                </Form.Item>
                {
                    itemData.studentUserId ? null : (
                        <Form.Item
                            label="登录密码"
                            name="password"
                            rules={[{ required: true, message: '必填' }]}
                        >
                            <Input placeholder="请输入登录密码"/>
                        </Form.Item>
                    )
                }
                <Form.Item
                    label="学员证号"
                    name="credentialsNo"
                    rules={[{ required: true, message: '必填' }]}
                >
                    <Input placeholder="请输入证件号"/>
                </Form.Item>

                {
                    itemData.studentUserId ? null : (
                        <>
                            <Form.Item
                                label="所在单位"
                                name="leastDepartmentId"
                                rules={[{ required: true, message: '必填' }]}
                            >
                                <Cascader
                                    options={treeData}
                                    placeholder="请选择所在单位"
                                />
                            </Form.Item>
                            <Form.Item
                                label="教学班号"
                                name="classNo"
                                rules={[{ required: true, message: '必填' }]}
                            >
                                <Select
                                    placeholder="教学班号"
                                >
                                    {
                                        teachClassList.map(item => {
                                            return <Option key={item.teachClassId}
                                                           value={item.teachClassId}>{item.classNo}</Option>;
                                        })
                                    }
                                </Select>
                            </Form.Item>


                        </>
                    )
                }
            </Form>
        </Modal>
    );
});

export default connect(({ students, dispatch }) => ({
    students,
    dispatch,
}))(UserModal);
